package cn.hibernatedemo.service;

import java.io.Serializable;

import org.hibernate.HibernateException;
import org.hibernate.Transaction;

import cn.hibernatedemo.dao.DeptDao;
import cn.hibernatedemo.dao.HibernateUtil;
import cn.hibernatedemo.entity.Dept;

public class DeptBiz {
	public void deleteDept(Dept dept) {
		Transaction tx = null;
		
		try {
			DeptDao dao = new DeptDao();
			tx = dao.currentSession().beginTransaction();
			
			dao.delete(dept);
			
			tx.commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			if (tx != null)
				tx.rollback();
		}
	}
	
	public void deleteDept(Serializable id) {
		Transaction tx = null;
		
		try {
			DeptDao dao = new DeptDao();
			tx = dao.currentSession().beginTransaction();
			
			dao.delete(id);
			
			tx.commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			if (tx != null)
				tx.rollback();
		}
	}
	
	public void updateDept(Dept dept) {
		Transaction tx = null;
		
		try {
			DeptDao dao = new DeptDao();
			tx = dao.currentSession().beginTransaction();
			
			dao.update(dept);
			
			tx.commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			if (tx != null)
				tx.rollback();
		}
	}
	
	public void modifyDept(Dept dept) {
		Transaction tx = null;

		try {
			DeptDao dao = new DeptDao();
			tx = dao.currentSession().beginTransaction();

			/*Dept deptToUpdate = dao.load(dept.getDeptno());
			deptToUpdate.setDname(dept.getDname());
			deptToUpdate.setLoc(dept.getLoc());*/
			dao.merge(dept);

			tx.commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			if (tx != null)
				tx.rollback();
		}
	}
	
	public Dept findById_load(Serializable id) {
		Transaction tx = null;
		Dept dept = null;
		try {
			DeptDao dao = new DeptDao();
			tx = dao.currentSession().beginTransaction();
			
			dept = dao.load(id);
			System.out.println(dept.getDname());
			
			tx.commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			if (tx != null)
				tx.rollback();
		}
		return dept;
	}
	
	public Dept findById_get(Serializable id) {
		Transaction tx = null;
		Dept dept = null;
		try {
			DeptDao dao = new DeptDao();
			tx = dao.currentSession().beginTransaction();

			dept = dao.get(id);

			tx.commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			if (tx != null)
				tx.rollback();
		}
		return dept;
	}

	public void addNewDept(Dept dept) {
		Transaction tx = null;

		try {
			DeptDao dao = new DeptDao();
			tx = dao.currentSession().beginTransaction();

			dao.save(dept);
			//dept.setDname("aaaa");

			tx.commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			if (tx != null)
				tx.rollback();
		}
	}

	public Dept mergeDept(Dept dept){
		Transaction tx = null;
		Dept persis = null;
		DeptDao dao = new DeptDao();
		try {
			tx = dao.currentSession().beginTransaction();

			persis = dao.merge(dept);

			tx.commit();
		}catch (HibernateException e){
			e.printStackTrace();
			if (tx != null)
				tx.rollback();
		}
		return persis;
	}
}
